import { Body, CacheInterceptor, CacheTTL, Controller, Get, Param, Post, Request, UseGuards, UseInterceptors } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { PostsService } from './posts.service';

@Controller('posts')
export class PostsController {
    constructor(private readonly postsService: PostsService) { }

    @Get('/')
    index() {
        return this.postsService.index()
    }

    @Get('/count')
    @UseInterceptors(CacheInterceptor)
    @CacheTTL(10) //Cache TTL can be set here
    async count() {
        return this.postsService.count()
    }

    @UseGuards(AuthGuard('jwt'))
    @UseInterceptors(CacheInterceptor)
    @CacheTTL(15) //Cache TTL can be set here
    @Get('/p/:pagenum')
    async page(@Param() param, @Request() req) {
        return this.postsService.page(param.pagenum, req.user)
    }

    @UseGuards(AuthGuard('jwt'))
    @Get('/view/:pid')
    viewPosts(@Param() param) {
        return this.postsService.viewPosts(param.pid)
    }

    @UseGuards(AuthGuard('jwt'))
    @UseInterceptors(CacheInterceptor)
    @CacheTTL(5) //Cache TTL can be set here
    @Get('/preview/:pid')
    PreviewPosts(@Param() param) {
        return this.postsService.PreviewPosts(param.pid)
    }

    @UseGuards(AuthGuard('jwt'))
    @UseInterceptors(CacheInterceptor)
    @CacheTTL(60) //Cache TTL can be longer at this page
    @Get('/view/:pid/:pagenum')
    viewReplyPage(@Param() param) {
        return this.postsService.viewReplyPage(param.pid, param.pagenum)
    }

    @UseGuards(AuthGuard('jwt'))
    @Post('/add')
    Addposts(@Request() req, @Body() post_json) {
        return this.postsService.Addpost(req.user, post_json)
    }

    @UseGuards(AuthGuard('jwt'))
    @Post('/reply/:pid')
    replyPosts(@Param() param, @Body() reply_json, @Request() req) {
        // console.log(req)
        return this.postsService.replyPost(param.pid, reply_json, req.user.userId)
    }

}
